﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Shopping Cart
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script language="javascript" type="text/javascript" >
// <![CDATA[

        function Test(field) {
            if (field.value < 1)
            {
                alert("Invalid !");
                field.value = 1;
                return false;
            }
        }

// ]]>
    </script>
    <h2>Your Cart</h2>
    <%
        if (Session["Cart"] == null)
        {
            Response.Write("<center><h2>Empty Cart</h2></center>");
        }
        else
        {
            GIFT_SHOP_MVC.Models.GiftShop_LinQDataContext data = new GIFT_SHOP_MVC.Models.GiftShop_LinQDataContext();
            long total=0;
            System.Data.DataTable cart = (System.Data.DataTable)Session["Cart"];
            Html.BeginForm("SaveChanges", "Cart");
            
            if (Session["OverAmount"]!=""&&Session["OverAmount"]!=null)
            {
                string [] id = Session["OverAmount"].ToString().Split(',');
                foreach (System.Data.DataRow row in cart.Rows)
                {
                    GIFT_SHOP_MVC.Models.SanPham product = data.GetProductById(int.Parse(row["Id"].ToString()));
                    long price = decimal.ToInt32(product.Gia) * int.Parse(row["Count"].ToString());
                    total += price;
                    Response.Write(product.TenSanPham);%>

                    <input type="text" id="<%=row["ID"].ToString()%>" name="<%=row["ID"].ToString()%>" value="<%=row["Count"].ToString()%>" onblur="return Test(this)" />
                    
                    <%
                    if (id.SingleOrDefault(i=>i==row["ID"].ToString())!=null)
                      {
                         %>
                            So luong mua vuot qua so luong hang dang co
                         <%
                         
                      } %>
                    So luong ton:<%=product.SL_Ton %>
                    <%=Html.TextBox("total", product.Gia.ToString())%>
                    <%=Html.ActionLink("Xóa", "DeleteCart/" + product.MaSanPham.ToString(), "Cart")%>
                    
                    <%Response.Write("<br />");
                }
            }
            else
            {
                 foreach (System.Data.DataRow row in cart.Rows)
                {
                    GIFT_SHOP_MVC.Models.SanPham product = data.GetProductById(int.Parse(row["Id"].ToString()));
                    long price = decimal.ToInt32(product.Gia) * int.Parse(row["Count"].ToString());
                    total += price;
                    Response.Write(product.TenSanPham);%>
                    <input type="text" id="<%=row["ID"].ToString()%>" name="<%=row["ID"].ToString()%>" value="<%=row["Count"].ToString()%>" onblur="return Test(this)" />
                    
                    So luong ton:<%=product.SL_Ton %>
                    <%=Html.TextBox("total", product.Gia.ToString())%>
                    <%=Html.ActionLink("Xóa", "DeleteCart/" + product.MaSanPham.ToString(), "Cart")%>
                
                    <%Response.Write("<br />");
                }
            }           
            %>
            <%=Html.TextBox("total",total.ToString()) %>
            <%=Html.ActionLink("Xóa hết", "DeleteCart/0", "Cart")%>
            <input type="submit" id="submit_btn" value="Save changes" />       
              
            <%
                if (Session["user"] != null)
                { %>

            <%=Html.ActionLink("Thanh toán", "Payment", "Cart")%>
            
            <%}
                else Response.Write("ban can dang nhap de thanh toan"); %>
        <%}
        %>
</asp:Content>
